package com.ruoyi.web.dorm.service.impl;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.dorm.entity.StudentHealthEntity;
import com.ruoyi.web.dorm.mapper.StuHealthMapper;
import com.ruoyi.web.dorm.service.StuHealthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StuHealthServiceImpl implements StuHealthService {

    @Autowired
    private StuHealthMapper stuHealthMapper;

    //    查询学生卫生
    @Override
    public List<StudentHealthEntity> getStuHealth(StudentHealthEntity studentHealthEntity) {
        return stuHealthMapper.getStuHealth(studentHealthEntity);
    }

    //添加学生卫生
    @Override
    public int addStuHealth(StudentHealthEntity studentHealthEntity) {
        String username = SecurityUtils.getLoginUser().getUsername(); //获取创建人的名字 添加进去
        studentHealthEntity.setCreateBy(username);
        Long studentId = studentHealthEntity.getStudentNum();
        studentHealthEntity.setStudentId(studentId);
        return stuHealthMapper.addStuHealth(studentHealthEntity);
    }

    //修改学生卫生信息
    @Override
    public int updateStuHealth(StudentHealthEntity studentHealthEntity) {
        String username = SecurityUtils.getLoginUser().getUsername(); //获取更新人的名字 添加进去
        studentHealthEntity.setUpdateBy(username);
        Long studentNum = studentHealthEntity.getStudentNum();

        //学号是拼出来的  截取出id 进行使用
        if (!StringUtils.isNull(studentNum)) { //判断学号是否有数据
            String strStudentId = String.valueOf(studentNum);
            Long studentId = Long.valueOf(strStudentId.substring(4));

            studentHealthEntity.setStudentId(studentId);

        }
        return stuHealthMapper.updateStuHealth(studentHealthEntity);
    }

    //假删除学生卫生信息
    @Override
    public int delStuHealth(Long id) {
        return stuHealthMapper.delStuHealth(id);
    }
}
